<?php
namespace app\common\behavior;

use app\common\lib\user_operate\ModuleOperate;
use app\common\model\PluginDbTableModel;

/**
 * 跨域请求  行为  绑定在app/tags.php  中
 */
class PluginUnInstallHook
{
    protected static $param; //参数

    protected static $operateList = [ //操作列表 , 顺序排列
        "uninstallDataTable", // step 1
        "uninstall", //step 2
    ];

    public function run(&$params)
    {
        self::$param = $params;

        $status = self::$param['info']['status'];

        $params['return'] = true;

        foreach (self::$operateList as $step) {

            if ($status != 4) {
                //开发中的应用跳过检测
                $step = end(self::$operateList);

                $params['return'] = $this->$step();

                break;
            }
            //按步骤执行
            $operateResult = $this->$step();

            if ($operateResult !== true) {

                $params['return'] = $operateResult;

                break;
            }
        }
    }

    /**
     * 卸载数据表
     */
    private function uninstallDataTable()
    {
        $name = self::$param['info']['name'];

        $dbTable = new PluginDbTableModel();
        //卸载数据表
        $res = @$dbTable->uninstallDataTable($name);

        $path = PLUGINS_PATH . cmf_parse_name($name);

        $res = $dbTable->delDirAndFile($path);

        return true;
    }

    /**
     * 执行卸载
     */
    private function uninstall()
    {
        $info = self::$param['info'];
        //除系统授权
        $MO = new ModuleOperate;

        $MO->storeId = $info['store_id'];

        $res = $MO->uninstall();

        if ($res['status'] != 'success') {

            return $res['message'];
        }
        return true;
    }

}
